<?php
class ml_biz_atNick_followList {
    private $oRds;
    const LATEST_AT_LIMIT = 10;
    const LATEST_EMPTY_UID = 5211519;
    
    public function __construct(){
        $this->oRds = new ml_model_atNick_rdsFollowList();
    }
    
    public function init_newFollow_redis($uid, $fuid){
        
        $re = $this->oRds->setFollowList($uid, $fuid);
        if (!$re) return $re;
            
        $re = $this->oRds->isMemberFollowList($fuid, $uid);
        if (!$re) {
            $re = $this->oRds->insertLatestFollow($uid, $fuid);
            $re = $this->oRds->trimLatestAt($uid, self::LATEST_AT_LIMIT+2);
        } else {
            $re = $this->oRds->insertLatestFriend($uid, $fuid);
            $re = $this->oRds->trimLatestAt($uid, self::LATEST_AT_LIMIT+2);
        }
        return true;
    }
    
    public function remove_Follow_redis($uid, $fuid){
        
        $re = $this->oRds->removeFollowList($uid,$fuid);
        if (!$re) return $re;
        $re = $this->oRds->removeLatestList_byUid($uid, $fuid);
        return $re;
    }
    
    public function update_latestAt_redis($uid, $atUid){
        $re = $this->oRds->isMemberFollowList($uid, $atUid);
        if (!$re) return false;
        $re = $this->oRds->removeLatestList_byUid($uid, $atUid);
        $re = $this->oRds->pushLatestAtUid($uid, $atUid);
        $re = $this->oRds->trimLatestAt($uid, self::LATEST_AT_LIMIT+2);
        
    }
    
    public function get_latestAt_redis($uid){
        
        $rs = $this->oRds->getLatestAt($uid, 0 , self::LATEST_AT_LIMIT+2);
        if (empty($rs)) {
            $rs = array(self::LATEST_EMPTY_UID);
        }
        
        $rs = array_filter ($rs, array($this, 'callback') );
        
        $aNicks = ml_tool_nick::uids2nicks($rs);
        return $aNicks;
    }
    
    private function callback($a) {
        return ($a != ml_model_atNick_rdsFollowList::PFX_SET_ATNICK_LATESTAT_FOLLOW) && 
               ($a != ml_model_atNick_rdsFollowList::PFX_SET_ATNICK_LATESTAT_FRIEND);
    }

}